package mesweb.mesweb.controller.pm;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import mesweb.mesweb.entity.pm.PMProduct;
import mesweb.mesweb.service.pm.IPMProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 产品类
 * @Author: jeecg-boot
 * @Date:   2021-12-17
 * @Version: V1.0
 */
@Slf4j
@Api(tags="产品类")
@RestController
@RequestMapping("/mesweb/pMProduct")
public class PMProductController {
	@Autowired
	private IPMProductService pMProductService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<PMProduct> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = pMProductService.list();
        } else {
            QueryWrapper<PMProduct> queryWrapper = new QueryWrapper<>();
            data = pMProductService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param pMProduct
	 * @param page
	 * @param size
	 * @param req
	 * @return
	 */
	@ApiOperation(value="产品类-分页列表查询", notes="产品类-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(PMProduct pMProduct,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<PMProduct> queryWrapper = new QueryWrapper<>();
		Page<PMProduct> pagePMProduct = new Page<PMProduct>(page, size);
		IPage<PMProduct> pageList = pMProductService.page(pagePMProduct, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param pMProduct
	 * @return
	 */
	@ApiOperation(value="产品类-编辑", notes="产品类-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody PMProduct pMProduct) {

       boolean result = false;
        if(pMProduct.getId() == 0L) {
			QueryWrapper<PMProduct> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("no", pMProduct.getNo());
			queryWrapper.or().eq("name", pMProduct.getName());
			PMProduct mesTechnologyFlow1 = pMProductService.getOne(queryWrapper);
			if (mesTechnologyFlow1 != null) {
				String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getNo(), mesTechnologyFlow1.getName());
				throw new ApiException(msg);
			}
             result = pMProductService.save(pMProduct);
        }
        else
        {
            result = pMProductService.updateById(pMProduct);
        }
        return CommonResult.success(result);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="产品类-通过id删除", notes="产品类-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		pMProductService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="产品类-批量删除", notes="产品类-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("PMProductIDs") @RequestBody List<Long> ids) {
		this.pMProductService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="产品类-通过id查询", notes="产品类-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		PMProduct pMProduct = pMProductService.getById(id);
		return CommonResult.success(pMProduct);
	}
}
